

const util = require('../../utils/util.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    showpopup: false,
    storesItem: {},
    storesItemValue: '',
    storeList: [],


    username: '',
    mobile: '',
    workPlaceParticular: '',
    remark: '',
    selectIndex3: '',
    selectLabel3: '',
    list3: [
      {
        name: '三口之家',
        id: 0
      },
      {
        name: '四口之家',
        id: 1
      },
      {
        name: '五口之家',
        id: 2
      },
      {
        name: '五口以上',
        id: 3
      },
    ],
    selectIndex4: '',
    selectLabel4: '',
    list4: [
      {
        name: '200平方以下',
        id: 0
      },
      {
        name: '300-500平方',
        id: 1
      },
      {
        name: '500平方以上',
        id: 2
      }
    ],
    selectIndex5: '',
    selectLabel5: '',
    list5: [
      {
        name: '佛教',
        id: 0
      },
      {
        name: '基督教',
        id: 1
      },
      {
        name: '伊斯兰教',
        id: 2
      },
      {
        name: '没要求',
        id: 3
      },
    ],
    selectIndex7: '',
    selectLabel7: '',
    list7: [
      {
        name: '7k-8k',
        id: 0
      },
      {
        name: '9k-10k',
        id: 1
      },
      {
        name: '11k-12k',
        id: 2
      },
      {
        name: '12k-15k',
        id: 3
      },
      {
        name: '15k以上',
        id: 4
      }
    ],

  },
  onPopupClick() {
    this.setData({
      showpopup: true,
    })
  },
  onPopupClose() {
    this.setData({
      showpopup: false,
    })
  },
  doPopupItem(e) {
    const data = e.currentTarget.dataset;
    this.setData({
      storesItem: data.item,
      showpopup: false,
      storesItemValue: data.item.address + '，' + data.item.name
    })
  },
  getStoresInit() {
    util.https('storesMgmt/storesQry', {
    }, (res) => {
      let storesBoList = res.storesBoList;
      this.setData({
        storeList: storesBoList
      })
    }, (res) => {
      wx.showModal({
        title: '温馨提示',
        content: res.errorMsg,
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
    })
  },
  clickItem3(e) {
    const data = e.currentTarget.dataset;
    this.setData({
      selectIndex3: data.index,
    })
  },
  clickItem4(e) {
    const data = e.currentTarget.dataset;
    this.setData({
      selectIndex4: data.index,
    })
  },
  clickItem5(e) {
    const data = e.currentTarget.dataset;
    this.setData({
      selectIndex5: data.index,
    })
  },
  clickItem7(e) {
    const data = e.currentTarget.dataset;
    this.setData({
      selectIndex7: data.index,
    })
  },

  onClickIcon() {
    wx.setStorageSync(
      'address_get_type',
      0
    );
    wx.navigateTo({ url: '/page5/addresslist/index' });
  },
  doSubmit() {
    let sData = this.data;
    console.log(sData)
    let postData = {
      "name": sData.username,
      "phone": sData.mobile,
      "remark": sData.remark,
      "workPlaceParticular": sData.workPlaceParticular,
      "faith": sData.selectIndex5 !== '' ? sData.list5[sData.selectIndex5].name : '',
      "familySize": sData.selectIndex3 !== '' ? sData.list3[sData.selectIndex3].name : '',
      "houseArea": sData.selectIndex4 !== '' ? sData.list4[sData.selectIndex4].name : '',
      "salary": sData.selectIndex7 !== '' ? sData.list7[sData.selectIndex7].name : '',

      "storesId": sData.storesItem.id,
      "storesName": sData.storesItem.name,
      "storesAddress": sData.storesItem.address,
    };
    if (postData.name == '' || postData.name == null) {
      wx.showModal({
        title: '温馨提示',
        content: '姓名不能为空！',
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
      return
    }
    if (!/^1[3456789]\d{9}$/.test(postData.phone)) {
      console.log('电话号码不正确！')
      wx.showModal({
        title: '温馨提示',
        content: '电话号码不正确！',
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
      return
    }
    let content = ''
    sData.selectIndex7 === '' ? content = '请选择薪资预期' : null;
    sData.selectIndex5 === '' ? content = '期望宗教信仰' : null;
    sData.selectIndex4 === '' ? content = '家庭面积' : null;
    sData.selectIndex3 === '' ? content = '服务家庭主要成员' : null;
    if (content) {
      wx.showModal({
        title: '温馨提示',
        content: '请选择' + content,
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
      return
    }
    if (postData.storesId == '' || postData.storesId == null) {
      wx.showModal({
        title: '温馨提示',
        content: '门店不能为空！',
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
      return
    }
    if (postData.workPlaceParticular == '' || postData.workPlaceParticular == null) {
      console.log('地址不能为空！')
      wx.showModal({
        title: '温馨提示',
        content: '地址不能为空！',
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
      return
    }
    util.https('domesticHousekeeper/domesticHousekeeperAdd', postData, (res) => {
      console.log(res)
      wx.redirectTo({
        url: '/page2/result/index',
      })
    }, (res) => {
      console.log(res)
      wx.showModal({
        title: '温馨提示',
        content: res.errorMsg,
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getStoresInit();
    wx.getStorage({
      key: 'address_get',
      success: (res) => {
        console.log(res)
        if (res.data) {
          let resData = res.data;
          this.setData({
            workPlaceParticular: resData.addrLine1 + "，" + resData.addrLine2,
          })
        } else {
          this.setData({
            workPlaceParticular: '',
          })
        }
      },
      fail: (res) => {
        console.log(res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */

})